package model

import (
	// "database/sql"
	. "github.com/klen-ygs/gorm-zero/gormc/sql"
	"gorm.io/gorm"
	"context"
	"github.com/klen-ygs/gorm-zero/gormc"
)

// avoid unused err
var _ = InitField
var _ MoneysModel = (*customMoneysModel)(nil)

type (
	// MoneysModel is an interface to be customized, add more methods here,
	// and implement the added methods in customMoneysModel.
	MoneysModel interface {
		moneysModel
		customMoneysLogicModel
	}

	customMoneysLogicModel interface {
		WithSession(tx *gorm.DB) MoneysModel
		Search(ctx context.Context, limit int, offset int) ([]Moneys, int32, error)
	}

	customMoneysModel struct {
		*defaultMoneysModel
	}
)

func (c customMoneysModel) WithSession(tx *gorm.DB) MoneysModel {
	newModel := *c.defaultMoneysModel
	c.defaultMoneysModel = &newModel
	c.conn = tx
	return c
}

// NewMoneysModel returns a model for the database table.
func NewMoneysModel(conn *gorm.DB) MoneysModel {
	return &customMoneysModel{
		defaultMoneysModel: newMoneysModel(conn),
	}
}

func (m *defaultMoneysModel) Search(ctx context.Context, limit int, offset int) ([]Moneys, int32, error) {
	var resp []Moneys
	var count int64
	filter := Moneys{
				Removed: false,
			}
	err := m.conn.WithContext(ctx).Model(&Moneys{}).Where(&filter).Count(&count).Offset(offset).Limit(limit).Find(&resp).Error

	if err == gormc.ErrNotFound {
		return nil, 0, err
	}
	// ret := make([]Dict, len(resp))
	// for i,item := range resp {
	// 	ret[i] = Dict{
	// 		"id"    :     item.Id,
	// 		"name"  :     item.Name,
	// 		"detail":     item.Detail,
	// 		"status":     item.Status,
	// 		"money" :     item.Money,
	// 		"typeID":     item.Types,
	// 		// "removed":    item.Removed,
	// 		"createtime": item.CreateTime.Format("2006-01-02 15:04:05"),
	// 	}
	// }
	return resp, int32(count), err
}

func (m *defaultMoneysModel) customCacheKeys(data *Moneys) []string {
	if data == nil {
		return []string{}
	}
	return []string{}
}
